from datetime import datetime, time
from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_admin.entity.do.config_do import SysConfig
from module_admin.entity.vo.config_vo import ConfigModel, ConfigPageQueryModel
from utils.page_util import PageUtil
from typing import Optional, List, Any


class ConfigDao:
    """
    参数配置管理模块数据库操作层
    """

    @classmethod
    async def get_config_detail_by_id(cls, db: AsyncSession,
                                      config_id: int) -> Optional[SysConfig]:
        """
        根据参数配置id获取参数配置详细信息

        :param db: orm对象
        :param config_id: 参数配置id
        :return: 参数配置信息对象
        """

        config_info = (await db.execute(
            select(SysConfig).where(SysConfig.config_id == config_id)
        )).scalar().first()
        return config_info

    @classmethod
    async def get_config_detail_by_key(
            cls, db: AsyncSession, config: ConfigModel) -> Optional[SysConfig]:
        """
        根据参数配置键名获取参数配置详细信息
        :param db: orm对象
        :param config: 参数配置对象
        :return: 参数配置信息对象
        """

        config_info = (await db.execute(
            select(SysConfig).where(
                SysConfig.config_key == config.config_key
                if config.config_key else True,
                SysConfig.config_value == config.config_value
                if config.config_value else True,
            ))).scalar().first()
        return config_info

    @classmethod
    async def get_config_list(cls, db: AsyncSession,
                              query_object: ConfigPageQueryModel,
                              is_page: bool) -> List[SysConfig]:
        """
        根据查询参数获取参数配置列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否分页
        :return: 参数配置列表信息

        """

        query = (select(SysConfig).where(
            SysConfig.config_name.like(f'%{query_object.config_name}%')
            if query_object.config_name else True,
            SysConfig.config_type.like(f'%{query_object.config_type}%')
            if query_object.config_type else True,
            SysConfig.config_key.like(f'%{query_object.config_key}%')
            if query_object.config_key else True,
            SysConfig.create_time.between(
                datetime.combine(
                    datetime.strftime(query_object.begin_time, '%Y-%m-%d'),
                    time(00, 00, 00)),
                datetime.combine(
                    datetime.strftime(query_object.end_time, '%Y-%m-%d'),
                    time(23, 59, 59)))
            if query_object.begin_time and query_object.end_time else True,
        ).distinct())

        config_list = await PageUtil.paginate(db,query=query, page_num=query_object.page_num,is_page=is_page, page_size=query_object.page_size)
        return config_list
)
